﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Xml.Linq;
using TestsModel;
using TestsModel.Parsers;
using TestUp.Models;
using TestUp.Models.Helpers;
using TestUp.Models.ShortEntities;
using TestUp.Models.UserModels;

namespace TestUp.Controllers
{
    public class UserController : Controller
    {
        public UserController()
        {
            
        }
  
        public ActionResult PendingTests()
        {
            UserInfo currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);
            if (currentUser != null)
            {
                var shortPendingTest = new List<ShortPendingTest>();
                currentUser.Group.PendingTests.ToList().ForEach(x => shortPendingTest.Add(x.ConvertToShortEntity()));
                return View(new PendingTestsModel
                    {
                        UserName = currentUser.UserName,
                        Permission = currentUser.Permission,
                        PendingTests = shortPendingTest
                    });
            }
            return RedirectToAction("Index", "Home");
        }
        public ActionResult PassingResults()
        {
            UserInfo currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);

            return View(new PassingResultsModel
                            {
                                UserName = currentUser.UserName,
                                Permission = currentUser.Permission,
                                PassingResults = currentUser.PassingResults.ToList()
                            });
        }
        public ActionResult OpenPendingTest(int? PendingTestId)
        {
            if (!PendingTestId.HasValue)
                return View("Error", string.Empty, @"No such test available!");

            var pendingTest = DataHelpers.PendingTestsHelper.GetPendingTest(PendingTestId.Value);
            var test = DataHelpers.TestHelper.GetTest(pendingTest.TestId);

            string testPath = DataHelpers.TestHelper.GetTestPath(test);
            if (System.IO.File.Exists(testPath))
            {
                var testData = XmlTestParser.ParseXmlFile(XDocument.Load(testPath));
                var model = new OpenTestModel
                    {
                        PendingTest = pendingTest,
                        TestData = testData.ClearAnswers()
                    };
                return View(model);
            }
            return View("Error", string.Empty, @"File for test not found!");
        }

        [HttpPost]
        public ActionResult SubmitTest(OpenTestModel model)
        {
            var currentUser = DataHelpers.UserInfoHelper.GetUserInfo(User.Identity.Name);
            if (currentUser != null)
            {
                var nativeTestData = DataHelpers.TestHelper.LoadTest(model.PendingTest.TestId);
                if (nativeTestData != null && model.TestData != null)
                {
                    var result = TestChecker.Check(nativeTestData, model.TestData);

                    DataHelpers.PassingResultHelper.CreatePassingResult(currentUser.UserId, model.PendingTest.TestId, result);
                    DataHelpers.PendingTestsHelper.DeletePendingTest(model.PendingTest.PendingTestId);

                    return RedirectToAction("PassingResults", "User");
                }
            }

            return RedirectToAction("Index", "Home");
        }
    }
}
